#!/bin/bash

PROJECT_ROOT_PATH="$(cd $(dirname "$0")/../ ; pwd)"
PROJECT_GIT_DIR_PATH="${PROJECT_ROOT_PATH}/.git"
PROJECT_DIR_NAME="$(basename ${PROJECT_ROOT_PATH})"

# if there is a .git directory at the project root then rely on git for the version string
if [ -r "${PROJECT_GIT_DIR_PATH}" ] ; then
    git describe --tags --abbrev --dirty --always
    exit 0
fi

# if the proejct root directory matches the naming convetion of an extracted archive then
# get the version number out of that
if [[ "${PROJECT_DIR_NAME}" =~ ^RTLSDR-Airband-[0-9]*\.[0-9]*\.[0-9]*$ ]]; then
    echo ${PROJECT_DIR_NAME} | cut -d '-' -f 3
    exit 0
fi

# print an error string to stderr (any output to stdout is considered success)
>&2 echo "did not find a git root directory at ${PROJECT_GIT_DIR_PATH} and failed to extract a version from ${PROJECT_DIR_NAME}"
